package com.yanwq.sayhi.controller.user.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.yanwq.sayhi.R;
import com.yanwq.sayhi.base.BaseActivity;
import com.yanwq.sayhi.base.BaseToolbarActivity;
import com.yanwq.sayhi.common.client.OkJsonObjectResponseCallback;
import com.yanwq.sayhi.common.client.OkRequestParams;
import com.yanwq.sayhi.common.client.OkRestClient;
import com.yanwq.sayhi.common.constant.RegexConstant;
import com.yanwq.sayhi.common.event.UserEvent;
import com.yanwq.sayhi.widget.MEditText;
import com.yanwq.sayhi.widget.MToast;

import java.util.regex.Pattern;

import butterknife.BindInt;
import butterknife.BindView;
import de.greenrobot.event.EventBus;

public class RegisterActivity extends BaseToolbarActivity {

    @BindView(R.id.register_username)
    MEditText mUsernameV;
    @BindView(R.id.register_password)
    MEditText mPasswordV;
    @BindView(R.id.register_confirm)
    MEditText mConfirmV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        EventBus.getDefault().register(this);
    }

    @Override
    public void init() {

    }

    @Override
    public void initView() {
        mToolbar.setTitle("注册");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_send, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        checkInfo();
        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @SuppressWarnings("unused")
    public void onEvent(UserEvent event) {
        switch (event.getEventType()) {
            case UserEvent.LOGIN:
                finish();
                break;
        }
    }

    private void checkInfo() {

        if (!Pattern.matches(RegexConstant.username, mUsernameV.getText().toString())) {
            MToast.showShort(this, "用户名格式错误");
            return;
        }

        if (!Pattern.matches(RegexConstant.password, mPasswordV.getText().toString())) {
            MToast.showShort(this, "密码格式错误");
            return;
        }

        if (!mPasswordV.getText().toString().equals(mConfirmV.getText().toString())) {
            MToast.showShort(this, "两次密码不一致");
            return;
        }

        OkRequestParams params = new OkRequestParams();
        params.put("username", mUsernameV.getText().toString());

        OkRestClient.getInstance().get(this, "/user/username", params, new OkJsonObjectResponseCallback() {

            @Override
            public void onStart() {
                showProgressDialog("检查用户名是否存在");
            }

            @Override
            public void onSuccess(JSONObject jsonObject) {
                if (!jsonObject.getBooleanValue("msg")) {
                    Intent intent = new Intent(RegisterActivity.this,
                            RegisterInfoActivity.class);
                    intent.putExtra("username", mUsernameV.getText().toString());
                    intent.putExtra("password", mPasswordV.getText().toString());
                    startActivity(intent);
                } else {
                    MToast.showShort(RegisterActivity.this, "用户名已存在");
                }
            }

            @Override
            public void onStop() {
                cancelProgressDialog();
            }
        });
    }
}
